using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX.Direct3D;

namespace Zsa.Emulation.Render.Video.Direct3d
{
    /// <summary>
    /// Settings used to create a direct 3d device
    /// </summary>
    public class Dx3dRenderSettings : GraphicRenderSettings
    {

        #region Fields
        /// <summary>
        /// The swap type.
        /// </summary>
        private SwapEffect _SwapEffect = SwapEffect.Discard;

        /// <summary>
        /// The device type.
        /// </summary>
        private DeviceType _DeviceType = DeviceType.Hardware;

        /// <summary>
        /// The create flags.
        /// </summary>
        private CreateFlags _CreateFlags = CreateFlags.SoftwareVertexProcessing;

        /// <summary>
        /// The texture format.
        /// </summary>
        private Format _TextureFormat = Format.Unknown;
        #endregion Fields

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="T:D3dRenderSettings"/> class.
        /// </summary>
        /// <param name="item">The item.</param>
        public Dx3dRenderSettings(RenderDeviceItem item)
            : base ( item )
        {
        }
        #endregion Constructor

        #region Properties
        /// <summary>
        /// Gets the index of the device.
        /// </summary>
        /// <value>The index of the device.</value>
        public int DeviceIndex
        {
            get
            {
                if ( base.DeviceItem.Id == null )
                {
                    throw new Exception();
                }
                if ( !(base.DeviceItem.Id is AdapterDetails) )
                {
                    throw new Exception();
                }
                AdapterDetails _Details =(AdapterDetails)base.DeviceItem.Id;
                return 0;
            }
        }

        /// <summary>
        /// Gets the type of the device.
        /// </summary>
        /// <value>The type of the device.</value>
        public DeviceType D3dDeviceType
        {
            get
            {
                return _DeviceType;
            }
        }

        /// <summary>
        /// Gets the create flags.
        /// </summary>
        /// <value>The create flags.</value>
        public CreateFlags CreateFlags
        {
            get
            {
                Caps _Caps = Manager.GetDeviceCaps(DeviceIndex, DeviceType.Hardware);
                if (_Caps.DeviceCaps.SupportsHardwareTransformAndLight)
                {
                    _CreateFlags = CreateFlags.HardwareVertexProcessing;
                }
                else
                {
                    _CreateFlags = CreateFlags.SoftwareVertexProcessing;
                }

                if (_Caps.DeviceCaps.SupportsPureDevice)
                {
                    _CreateFlags |= CreateFlags.PureDevice;
                }
                return _CreateFlags;
            }
        }



        /// <summary>
        /// Gets the present param.
        /// </summary>
        /// <value>The present param.</value>
        public PresentParameters PresentParam
        {
            get
            {
                PresentParameters _Param = new PresentParameters();
                _Param.Windowed = true;//!IsFullScreen;
                _Param.SwapEffect = _SwapEffect;
                //_Param.BackBufferFormat = _TextureFormat;
                _Param.AutoDepthStencilFormat = DepthFormat.D16;
                _Param.EnableAutoDepthStencil = true;
                _Param.BackBufferCount = 1;

                return _Param;
            }
        }

        #endregion Properties


        public override GraphicRender CreateDevice()
        {
            return new Dx3dGraphicRender(this);
            
        }

        public override bool IsValid()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override GraphicRenderCapabilities Capabilities
        {
            get 
            {
                return new Dx3dRenderCapabilities();
            }
        }

        public Format TextureFormat
        {
            get
            {
                return _TextureFormat;
            }
        }

        /// <summary>
        /// Equalses the specified other.
        /// </summary>
        /// <param name="other">The other.</param>
        /// <returns></returns>
        public override bool Equals(GraphicRenderSettings other)
        {
            Dx3dRenderSettings _RenderSettings = other as Dx3dRenderSettings;
            return false;
            
        }

    }
}
